<template>
    <div class="page-password">
        <van-nav-bar class="password-tilte" left-arrow :title="$t('m.Password')" @click-left="$router.back(-1)">
        </van-nav-bar>
        <div class="password-box">
            <label for="password1" class="label-class">{{$t('m.Newpassword')}}</label>
            <div class="password-inside-box">
                <input type="text" v-model="password" @input="setPasswordError" id="password1" v-bind:class="{'text-danger': passwordError }" class="input-class password-input" />
            </div>
            <!--<span v-if="passwordError" style="color: #fa5555;">At least 8 or more characters.</span>-->
            <p class="password-txt">{{$t('m.Atleastormorecharacters')}}</p>
            <p class="password-txt">{{$t('m.Atleastnumber')}}</p>
            <div class="submit-btn-box">
                <van-button size="large" class="submit-btn" :loading="btnLoading" @click="editPassword()">{{$t('m.Save')}}</van-button>
            </div>
        </div>

    </div>
</template>
<script>
    import {setPassword} from '../../static/api/mgc_api'
    import Qs from 'qs'
    export default {
        name: 'Password',
        middleware: ['login'],
        data() {
            return {
                password: '',
                btnLoading: false,
                passwordError: false
            };
        },
        methods: {
            setPasswordError() {
                let reg = /^(?!\D+$).{8,50}$/
                if (this.password.match(reg)) {
                    this.passwordError = false
                }
            },
            editPassword() {
                let regPassword = /^(?!\D+$).{8,50}$/
                let password = this.$translateLang(this.password,'zawgyiTounicode')
                if (!password.match(regPassword)) {
                    this.passwordError = true
                    return false
                }
                let data = {
                    'newPassword': this.$translateLang(this.password,'zawgyiTounicode'),
                }
                let params = Qs.stringify(data)
                this.btnLoading = true
                setPassword(params).then((res) => {
                    if (res.data) {
                        if (res.data.errorID === 0) {
                            this.$toast.success('Successful')
                            this.$router.back(-1)
                        } else {
                            this.$toast(res.data.message)
                        }
                        this.btnLoading = false
                    }
                })
                    .catch((error) => {
                        console.log(error)
                        this.$toast('error,please try again leter')
                        this.btnLoading = false
                    })
            },
            translateString(str){
                if (!str) return ''
                return this.$translateLang(str, 'unicodeTozawgyi');
            },
        }
    }

</script>
<style>
    .password-tilte.van-nav-bar{
        background-color: #f2f2f2;
    }
    .password-box.van-nav-bar .van-icon{
        color: #bdbdbd;
    }
    .password-box .van-cell-group{
        height: 78%;
        margin: 0 4%;
    }
    .password-box .van-hairline--top-bottom:after{
        border: 1px solid #2c3e50;
    }
    .password-box .van-cell,.description-box .van-field__body, .description-box .van-field--min-height .van-field__control{
        height: 100%;
    }
    .password-box .submit-btn.van-button--large{
        height: 46px;
        line-height: 44px;
        border: 1px solid #f90;
        background: #f90;
        border-radius: 5px;
        color: #fff;
    }
    .password-box .van-nav-bar__title{
        color: #000;
    }
</style>
<style scoped>
    .password-box{
        padding: 0 4%;
        margin-top: 15px;
    }
    .input-class {
        -webkit-appearance: none;
        background-color: #fff;
        border: 1px solid #d8dce5;
        box-sizing: border-box;
        color: #5a5e66;
        display: inline-block;
        font-size: inherit;
        height: 40px;
        line-height: 1;
        outline: 0;
        padding: 0 15px;
        transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
        width: 100%;
    }

    .input-class.text-danger {
        border: 1px solid #fa5555;
    }

    input {
        text-rendering: auto;
        color: initial;
        letter-spacing: normal;
        word-spacing: normal;
        text-transform: none;
        text-indent: 0px;
        text-shadow: none;
        display: inline-block;
        text-align: start;
        margin: 0em;
        font: 400 13.3333px Arial;
        -webkit-appearance: textfield;
        background-color: white;
        -webkit-rtl-ordering: logical;
        cursor: text;
        padding: 1px;
        border-width: 2px;
        border-style: inset;
        border-color: initial;
        border-image: initial;
    }
    .label-class {
        text-align: left;
        vertical-align: middle;
        float: left;
        font-size: 14px;
        color: #5a5e66;
        line-height: 32px;
        padding: 0 12px 0 0;
        box-sizing: border-box;
    }

    .label-class:after {
        content: "*";
        color: #fa5555;
        margin-left: 4px;
    }
    .password-txt{
        margin: 5px 0;
    }
    .submit-btn-box{
        margin-top: 30px;
    }
</style>
